package com.ruoyi.information.controller.back;

import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.information.domain.back.AboutBack;
import com.ruoyi.information.service.back.IAboutBackService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 关于我们Controller
 * 后台
 * @author ruoyi
 * @date 2023-05-04
 */
@RestController
@RequestMapping("/aboutBack")
public class AboutBackController extends BaseController {
    @Autowired
    private IAboutBackService aboutService;

    /**
     * 查询关于我们列表
     */
    @RequiresPermissions("information:about:list")
    @GetMapping("/list")
    public TableDataInfo list(AboutBack about) {
        startPage();
        List<AboutBack> list = aboutService.selectAboutList(about);
        return getDataTable(list);
    }

    /**
     * 导出关于我们列表
     */
    @RequiresPermissions("information:about:export")
    @Log(title = "关于我们", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AboutBack about) {
        List<AboutBack> list = aboutService.selectAboutList(about);
        ExcelUtil<AboutBack> util = new ExcelUtil<AboutBack>(AboutBack.class);
        util.exportExcel(response, list, "关于我们数据");
    }

    /**
     * 获取关于我们详细信息
     */
    @RequiresPermissions("information:about:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(aboutService.selectAboutById(id));
    }

    /**
     * 新增关于我们
     */
    @RequiresPermissions("information:about:add")
    @Log(title = "关于我们", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AboutBack about) {
        AboutBack about1 = new AboutBack();
        about1.setTitle(about.getTitle());
        List<AboutBack> abouts = aboutService.selectAboutList(about1);
        if (abouts.size() > 0) {
            return AjaxResult.error("同样的类型只允许添加一条");
        }
        return toAjax(aboutService.insertAbout(about));
    }

    /**
     * 修改关于我们
     */
    @RequiresPermissions("information:about:edit")
    @Log(title = "关于我们", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AboutBack about) {

        AboutBack selected = aboutService.selectAboutById(about.getId());
        if (!selected.getTitle().equals(about.getTitle())){

            AboutBack about1 = new AboutBack();
            about1.setTitle(about.getTitle());
            List<AboutBack> abouts = aboutService.selectAboutList(about1);
            if (abouts.size() > 0) {
                return AjaxResult.error("同样的类型只允许存在一条");
            }
        }
        return toAjax(aboutService.updateAbout(about));
    }

    /**
     * 删除关于我们
     */
    @RequiresPermissions("information:about:remove")
    @Log(title = "关于我们", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(aboutService.deleteAboutByIds(ids));
    }
}
